import { UIComponent, UIMgr } from '../../../Framework/Scripts/Managers/UIMgr';
import { _decorator, Component, Node, Label, view, Vec3, UITransform, director, sys, native } from 'cc';
import { GameApp } from '../GameApp';
import { UserData } from '../userData/UserData';
import { EventMgr } from '../../../Framework/Scripts/Managers/EventMgr';
import { constant } from '../constant';
import { SoundMgr } from '../../../Framework/Scripts/Managers/SoundMgr';
import { ResMgr } from '../../../Framework/Scripts/Managers/ResMgr';

const { ccclass, property } = _decorator;


/** 
  周无邪出品
  本源码用于学习研究使用。
  作者联系方式：tuya7078（微信）。
 */
@ccclass('UIhome_UICtrl')
export class UIhome_UICtrl extends UIComponent {

    private gold: Label = null
    private ore: Label = null

    //  private level: Label = null




    start() {


    }


    onLoad() {





        SoundMgr.Instance.playBgMusic(ResMgr.Instance.getAsset("Sounds", "bgm1"), true)
        if (UIMgr.Instance.canvasNode.getChildByName("bgMask")) {
            UIMgr.Instance.canvasNode.getChildByName("bgMask").destroy()
        }
        GameApp.Instance.isBack = false
        // this.level = this.ViewNode("top/level").getComponent(Label)
        //  this.level.string = UserData.Instance.localData["level"]
        this.gold = this.ViewNode("top/gold/Label").getComponent(Label)
        this.ore = this.ViewNode("top/ore/Label").getComponent(Label)

        this.upHomeTop("", null)

        EventMgr.Instance.AddEventListener(constant.EVENT_NAME.upHomeTop, this.upHomeTop, this)
        //屏幕适配
        const visibleSize: number = view.getVisibleSize().height
        console.log("画布高度" + visibleSize);


        this.ViewNode("top").position = new Vec3(this.ViewNode("top").position.x, (visibleSize / 2) - this.ViewNode("top").getComponent(UITransform).height / 2, 0)
        const topPOs: Vec3 = this.ViewNode("top").position
        this.ViewNode("logo").position = new Vec3(this.ViewNode("logo").position.x, topPOs.y - 300, 0)
        this.ViewNode("startBtn").position = new Vec3(this.ViewNode("startBtn").position.x, (-(visibleSize / 2)) + this.ViewNode("startBtn").getComponent(UITransform).height / 2 + 500, 0)

        this.ViewNode("foot").position = new Vec3(this.ViewNode("foot").position.x, (-(visibleSize / 2)) + this.ViewNode("foot").getComponent(UITransform).height / 2, 0)


        this.AddButtonListener("startBtn", this, () => {
            if (!GameApp.Instance.isBack) {
                SoundMgr.Instance.playSoundOneShot(ResMgr.Instance.getAsset("Sounds", "click"))
                SoundMgr.Instance.stopBgMusic()
                // GameApp.Instance.showLoadUI()
                GameApp.Instance.isBack = true
                GameApp.Instance.StartGame()
            }

        })
        this.AddButtonListener("logo/setBtn", this, () => {
            if (!GameApp.Instance.isBack) {
                SoundMgr.Instance.playSoundOneShot(ResMgr.Instance.getAsset("Sounds", "click"))

                UIMgr.Instance.showUIView("set")


            }
        })
        this.AddButtonListener("shopBtn", this, () => {
            if (!GameApp.Instance.isBack) {
                SoundMgr.Instance.playSoundOneShot(ResMgr.Instance.getAsset("Sounds", "click"))

                GameApp.Instance.isBack = true
                UIMgr.Instance.showUIView("shop_player")
            }
        })

        this.AddButtonListener("friendlyBtn", this, () => {
            if (!GameApp.Instance.isBack) {
                SoundMgr.Instance.playSoundOneShot(ResMgr.Instance.getAsset("Sounds", "click"))

                GameApp.Instance.isBack = true
                UIMgr.Instance.showUIView("shop_friendly")
            }

        })

        this.AddButtonListener("buildBtn", this, () => {
            if (!GameApp.Instance.isBack) {
                SoundMgr.Instance.playSoundOneShot(ResMgr.Instance.getAsset("Sounds", "click"))

                GameApp.Instance.isBack = true
                UIMgr.Instance.showUIView("shop_build")
            }

        })
        //关卡测试按钮
        // this.AddButtonListener("levelBtn", this, () => {

        //     UserData.Instance.localData["level"] += 10
        //     UserData.Instance.saveLocalData()
        //     this.level.string = UserData.Instance.localData["level"]
        // })

        this.AddButtonListener("qiandaoBtn", this, () => {
            if (!GameApp.Instance.isBack) {
                SoundMgr.Instance.playSoundOneShot(ResMgr.Instance.getAsset("Sounds", "click"))

                //   GameApp.Instance.showLoadUI()
                GameApp.Instance.isBack = true
                UIMgr.Instance.showUIView("qiandao")
            }


        })
        this.AddButtonListener("choujiangBtn", this, () => {
            if (!GameApp.Instance.isBack) {
                SoundMgr.Instance.playSoundOneShot(ResMgr.Instance.getAsset("Sounds", "click"))

                //    GameApp.Instance.showLoadUI()
                GameApp.Instance.isBack = true
                UIMgr.Instance.showUIView("choujiang")
            }



        })


    }






    onDestroy() {
        EventMgr.Instance.RemoveEventListener(constant.EVENT_NAME.upHomeTop, this.upHomeTop, this)

    }

    private upHomeTop(str, data) {
        this.gold.string = "金币：" + UserData.Instance.localData["gold"]
        this.ore.string = "宝石：" + UserData.Instance.localData["ore"]
    }
    update(deltaTime: number) {

    }
}


